package com.ycomputer.pizzashow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

import com.ycomputer.adapter.TextMenuAdapter;
import com.ycomputer.dialogactivity.SetPasswordActivity;
import com.ycomputer.utils.StringUtils;

public class MainActivity extends BaseActivity {

	//activity result code
	private static final int ACTIVITY_REQUESTCODE_SETPASSWORD = 0;
	private static final int ACTIVITY_REQUESTCODE_CONFIRM = 1;
	//menu type
	private static final int MENU_TYPE_TEXT = 20;
	private static final int MENU_TYPE_PAGE = 21;
	//view
	private ListView textMenuColumn1;
	private ListView textMenuColumn2;
	private ListView textMenuColumn3;
	//adapter
	private TextMenuAdapter textMenuColumn1Adapter;
	private TextMenuAdapter textMenuColumn2Adapter;
	private TextMenuAdapter textMenuColumn3Adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 密码对话框处理
		setPassword();
		// set menu
		int menuType = getMenuType();
		setMenu(menuType);
		
	}

	private void setMenu(int menuType) {
		if(menuType == MENU_TYPE_TEXT){
			initTextMenu();
		}else if(menuType == MENU_TYPE_PAGE){
			
		}
		
	}

	private void initTextMenu() {
		textMenuColumn1 = (ListView) findViewById(R.id.text_menu_column1);
		textMenuColumn1Adapter = new TextMenuAdapter(this, getTextMenuData(1), R.layout.text_menu_column_item);
		textMenuColumn1.setAdapter(textMenuColumn1Adapter);
		
		textMenuColumn2 = (ListView) findViewById(R.id.text_menu_column2);
		textMenuColumn2Adapter = new TextMenuAdapter(this, getTextMenuData(2), R.layout.text_menu_column_item);
		textMenuColumn2.setAdapter(textMenuColumn2Adapter);
		
		textMenuColumn3 = (ListView) findViewById(R.id.text_menu_column3);
		textMenuColumn3Adapter = new TextMenuAdapter(this, getTextMenuData(3), R.layout.text_menu_column_item);
		textMenuColumn3.setAdapter(textMenuColumn3Adapter);
	}

	private List<? extends Map<String, ?>> getTextMenuData(int column) {
		if(column == 1){
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			return list;
		}else if(column == 2){
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			return list;
		}else if(column == 3){
			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);
			
			map = new HashMap<String, Object>();
			map.put("name", "Deep fried Calamari");
			map.put("price", "$11.00");
			map.put("desc", "Served with lemon tartare sauce");
			list.add(map);

			return list;
		}
		return null;
	}

	private int getMenuType() {
		return MENU_TYPE_TEXT;
	}

	private void setPassword() {
		SharedPreferences passwordPreferences= this.getSharedPreferences("passwordPreferences", Context.MODE_PRIVATE);
		String password = passwordPreferences.getString("password", "");
		Bundle bundle = new Bundle();
		if(StringUtils.isEmptyString(password)){
			//弹出设置密码对话框
	        bundle.putInt("type", 0);
			eStartActivityForResult(SetPasswordActivity.class, bundle, ACTIVITY_REQUESTCODE_SETPASSWORD);
		}else{
			//弹出输入密码对话框
			bundle.putInt("type", 1);
			eStartActivityForResult(SetPasswordActivity.class, bundle, ACTIVITY_REQUESTCODE_CONFIRM);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case ACTIVITY_REQUESTCODE_SETPASSWORD:
			//判断用户名密码是否设置成功，成功则管理员模式，否则普通用户模式
			SharedPreferences passwordPreferences= this.getSharedPreferences("passwordPreferences", Context.MODE_PRIVATE);
			String password = passwordPreferences.getString("password", "");
			if(!StringUtils.isEmptyString(password)){//成功则管理员模式
				setDisplayModel(passwordPreferences, true);
			}else{//否则普通用户模式
				setDisplayModel(passwordPreferences, false);
			}
			break;
		case ACTIVITY_REQUESTCODE_CONFIRM:
			//如果是点击 ok 初始化主页为管理员模式
			SharedPreferences preferences= this.getSharedPreferences("passwordPreferences", Context.MODE_PRIVATE);
			if(resultCode == 0){
				setDisplayModel(preferences,true);
			}else{//如果点击的是cancel，初始化为普通用户模式
				setDisplayModel(preferences,false);
			}
			break;

		default:
			break;
		}
		
		
	}

	private void setDisplayModel(SharedPreferences preferences, boolean model) {
		Editor editor = preferences.edit();
		editor.putBoolean("display_modle", model);
		editor.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
